package thread.JUC;

import java.util.concurrent.CountDownLatch;

public class Demo3 {
    public static void main(String[] args) {
        int taskCount = 10;
        CountDownLatch latch = new CountDownLatch(taskCount);

        Runnable task = () -> {
            // 模拟任务的执行
            System.out.println("任务正在执行。");
            // 任务完成后减少计数器
            latch.countDown();
        };

        // 启动 10 个线程执行任务
        for (int i = 0; i < taskCount; i++) {
            Thread thread = new Thread(task);
            thread.start();
        }

        try {
            // 等待所有任务完成
            latch.await();
            System.out.println("所有任务已完成。主线程可以继续执行。");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
